//
//  GameCenterViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/2/27.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "GameCenterViewController.h"
#import "XHomeRecommendedCell.h"
#import "XHomeItemCell.h"
#import "FoundServer.h"
#import "XHomeSegementView.h"
#import "XHSearchBar.h"
#import "ScatterDappManager.h"

static NSString *const kTipSectionViewIdentifier = @"kTipSectionViewIdentifier";

@interface GameCenterViewController ()<UISearchBarDelegate>
@property (nonatomic,strong) FoundServer *server;

@end

@implementation GameCenterViewController
- (instancetype)init{
    if (self = [super init]) {
        self = [super initWithStyle:UITableViewStylePlain];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadMore:(BOOL)isMore{
    [self.server getCenterListIsMore:isMore completion:^(BOOL success, BOOL isHaveNextPage, NSString * _Nonnull msg) {
        [self endHeaderFooterRefreshingIsHaveNextPage:isHaveNextPage];
        if (success) {
            [self.tableView reloadData];
        }else{
            [self.view makeToast:msg];
        }
    }];
}

- (void)setupUI{
    self.title = XLocalized(@"游戏中心");
    [self.tableView registerClass:XHomeTipHeadView.class forHeaderFooterViewReuseIdentifier:kTipSectionViewIdentifier];
    self.tableView.tableFooterView = [UIView new];
    UIView*headV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    self.tableView.tableHeaderView = headV;
    XHSearchBar *searchBar = [[XHSearchBar alloc]initWithFrame:CGRectMake(kNormalSpace, 4, kScreenWidth-2*kNormalSpace, 36)];
    [headV addSubview:searchBar];
    searchBar.delegate = self;
}

#pragma mark - table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.server.recommendedList.count?1:0;
    }else{
        return self.server.gamesList.count;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    kWeakSelf(self);
    if (section==0) {
        XHomeRecommendedCell *recommendedCell = [XHomeRecommendedCell cellForTableView:tableView];
        recommendedCell.hasLine = YES;
        recommendedCell.data = self.server.recommendedList;
        recommendedCell.ClickBlock = ^(FoundRecommendModel * _Nonnull model) {
            [ScatterDappManager manager:weakself forDapp:model];
        };
        return recommendedCell;
    }else{
        XHomeItemCell *cell = [XHomeItemCell cellForTableView:tableView];
        FoundRecommendModel*m = [self.server.gamesList objectAtIndex:indexPath.row];
        [cell.imageV sd_setImageWithURL:String_To_URL(m.logo) placeholderImage:nil];
        cell.titleLab.text = m.name;
        cell.subLab.text = m.introduction;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    if (section==0) {
        if (self.server.recommendedList.count%4==0) {
            return kHomeRecommendedHeight*(self.server.recommendedList.count/4)+kHomeRecommendedTopBottomSpace;
        }else{
            return kHomeRecommendedHeight*(self.server.recommendedList.count/4+1)+kHomeRecommendedTopBottomSpace;
        }
    }
    return kXHomeItemCellHeight;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    XHomeTipHeadView *recommendedTipView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kTipSectionViewIdentifier];
    if (section == 0) {
        recommendedTipView.titleLab.text = XLocalized(@"推荐Dapps");
        if (!self.server.recommendedList.count) {
            return [UIView new];
        }
    }else{
        recommendedTipView.titleLab.text = XLocalized(@"所有Dapps");
        if (!self.server.gamesList.count) {
            return [UIView new];
        }
    }
    return recommendedTipView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return self.server.recommendedList.count?KHomeSegementHeight:0.01f;
    }
    return self.server.gamesList.count?KHomeSegementHeight:0.01f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [ScatterDappManager manager:self forDapp:self.server.gamesList[indexPath.row]];
}
#pragma mark - searchDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    DLog(@"searchText %@",searchText);
    self.server.key = searchText;
    [self loadMore:NO];
}
#pragma mark - getter
- (FoundServer*)server{
    if (!_server) {
        _server = [FoundServer new];
    }
    return _server;
}
@end

